package com.buaa.pm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buaa.pm.entity.Paragraph;
import com.buaa.pm.mapper.ParagraphMapper;
import com.buaa.pm.service.ParagraphService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ParagraphServiceImpl extends ServiceImpl<ParagraphMapper, Paragraph> implements ParagraphService {

    @Override
    public List<Paragraph> listParagraphByProject(String projectId) {
        QueryWrapper<Paragraph> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Paragraph::getProjectId, projectId);
        queryWrapper.lambda().orderByDesc(Paragraph::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public List<Paragraph> listParagraphByPaper(String paperId) {
        QueryWrapper<Paragraph> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Paragraph::getPaperId, paperId);
        queryWrapper.lambda().orderByDesc(Paragraph::getCreatedAt);
        return list(queryWrapper);
    }

    @Override
    public boolean updateParagraphAvailable(String paragraphId, boolean available) {
        UpdateWrapper<Paragraph> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Paragraph::getId, paragraphId).set(Paragraph::getAvailable, available);
        return update(updateWrapper);
    }
}
